// Ch_Op_St.cpp : implementation file
//

#include "stdafx.h"
#include "VolDia.h"
#include "Ch_Op_St.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CVolDiaApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CCh_Op_St dialog


CCh_Op_St::CCh_Op_St(CWnd* pParent /*=NULL*/)
	: CDialog(CCh_Op_St::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCh_Op_St)
	m_CurrSt = _T("");
	m_startX = 0.0f;
	m_startY = 0.0f;
	//}}AFX_DATA_INIT

	m_NextStInt = 0;
}


void CCh_Op_St::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCh_Op_St)
	DDX_Control(pDX, ID_SET_CURRENT, m_btnSetActal);
	DDX_Control(pDX, ID_SET_COORD, m_btnSetCoord);
	DDX_Control(pDX, IDC_CMB_OP, m_cmbOp);
	DDX_Control(pDX, IDC_CMB_NEXT_ST, m_cmbNextSt);
	DDX_Text(pDX, IDC_CURR_ST, m_CurrSt);
	DDX_Text(pDX, IDC_START_X, m_startX);
	DDX_Text(pDX, IDC_START_Y, m_startY);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCh_Op_St, CDialog)
	//{{AFX_MSG_MAP(CCh_Op_St)
	ON_CBN_SELCHANGE(IDC_CMB_NEXT_ST, OnSelchangeCmbNextSt)
	ON_CBN_SELCHANGE(IDC_CMB_OP, OnSelchangeCmbOp)
	ON_BN_CLICKED(ID_SET_CURRENT, OnSetCurrent)
	ON_BN_CLICKED(ID_SET_COORD, OnSetCoord)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCh_Op_St message handlers

BOOL CCh_Op_St::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	RETCOND			retCnd;
	RETSTAT			retSt;
	int				currPhas;
	int				rec;
	FILEIDX			fileMice;
	micro_cmd_exe	mice;
	TCHAR			temp[32];
	int				pos;
	int				toSel;

	fileMice.num = F_MICE;

	retSt = GetDynCurrentPhase(&currPhas);
	if(retSt != RT_NORMAL)
		return(FALSE);

	this->m_cmbOp.ResetContent();
	this->m_cmbNextSt.ResetContent();

	pos = 0;
	toSel = 0;
	for(rec = 1; rec <= N_MAX_MICENR; rec++){
		retCnd = FileRead(&fileMice, rec, 1, &mice, sizeof(mice));
		if(retCnd != R_NORMAL)
			return(FALSE);
		if(mice.phase == currPhas){
			_stprintf_s(temp, _T("%d"), mice.index);
			pos = this->m_cmbOp.AddString(temp);
			if((mice.exe != MICE_SUCCESS) && (toSel == 0))
				toSel = pos;

		}
	}

	this->m_cmbOp.SetCurSel(toSel);
	
	this->m_cmbNextSt.InsertString(MICE_PENDING, _T("To be executed"));
	this->m_cmbNextSt.InsertString(MICE_SUCCESS, _T("Completed"));
	this->m_cmbNextSt.InsertString(MICE_INPROGR, _T("In Progress"));
	this->m_cmbNextSt.InsertString(MICE_FAILED, _T("Failed"));
	this->m_cmbNextSt.InsertString(MICE_STOPPED, _T("Stopped"));
	
	this->m_cmbNextSt.SetCurSel(0);
	this->m_cmbOp.SetCurSel(0);
	
	OnSelchangeCmbOp();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCh_Op_St::OnSelchangeCmbNextSt() 
{

	this->m_NextStInt = this->m_cmbNextSt.GetCurSel();
}

void CCh_Op_St::OnSelchangeCmbOp(){
	RETSTAT			retSt;
	TCHAR			temp[32];
	short int		miceIndex;
	int				loggedUser;
	micro_cmd_exe	mice;

	this->m_cmbOp.GetLBText(this->m_cmbOp.GetCurSel(), temp);
	miceIndex = _ttoi(temp);

	retSt = GetMice(miceIndex, &mice);
	if(retSt != RT_NORMAL)
		return;
	
	switch(mice.exe){
	case MICE_PENDING:
		this->m_CurrSt.Format(_T("To be executed"));
		break;
	case MICE_SUCCESS:
		this->m_CurrSt.Format(_T("Completed"));
		break;
	case MICE_INPROGR:
		this->m_CurrSt.Format(_T("In Progress"));
		break;
	case MICE_FAILED:
		this->m_CurrSt.Format(_T("Failed"));
		break;
	case MICE_BREAK:
		this->m_CurrSt.Format(_T("Interrupted"));
		break;
	case MICE_STOPPED:
		this->m_CurrSt.Format(_T("Stopped"));
		break;
	}
	
	this->m_startX = mice.startPX;
	this->m_startY = mice.startPY;

	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &loggedUser);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unknown logged user."), _T("Error"), MB_OK);
		return;
	}

	if((mice.startPX == 0) && (mice.startPY == 0)){
		this->m_btnSetActal.EnableWindow(true);
		this->m_btnSetCoord.EnableWindow(true);
	}
	else{
		switch(loggedUser){
		case SYSMGR:
		case LEVEL1:
		case LEVEL2:
		case LEVEL3:
			this->m_btnSetActal.EnableWindow(true);
			this->m_btnSetCoord.EnableWindow(true);
			break;
		case LEVEL4:
		case LEVEL5:
			this->m_btnSetActal.EnableWindow(false);
			this->m_btnSetCoord.EnableWindow(false);
			break;
		default:
			this->m_btnSetActal.EnableWindow(false);
			this->m_btnSetCoord.EnableWindow(false);
			break;
		}
	
	}

	UpdateData(false);	
}

void CCh_Op_St::OnOK() 
{
	RETSTAT		retSt;
	int			currOp;
	TCHAR		temp[32];
	int			phase;
	short int	nextToExe;

	retSt = GetDynCurrentPhase(&phase);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Cannot retrieve current phase"), _T("Error"), MB_OK | MB_ICONINFORMATION);
		return;
	}
	
	if(phase == 0){
		retSt = GetDynNextPhase(&phase);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Cannot retrieve next phase"), _T("Error"), MB_OK | MB_ICONINFORMATION);
			return;
		}
	}

	this->m_cmbOp.GetLBText(this->m_cmbOp.GetCurSel(), temp);
	currOp = _ttoi(temp);

	retSt = SetMiceStatus(currOp, this->m_NextStInt);
	if(retSt != RT_NORMAL)
		return;

	retSt = GetMiceFirstToExe(&nextToExe);
	if(retSt == RT_NORMAL){
		theApp.m_pDlg->m_pElemMoveDialog->m_ProBarTc.SetPos(nextToExe);
	}

	OnSelchangeCmbOp();
	theApp.m_pDlg->m_pElemMoveDialog->ElemListUpdate(currOp);
	theApp.m_pDlg->m_pElemMoveDialog->ElemEditUpdate(0);
	theApp.m_pDlg->m_pElemMoveDialog->TCOpListUpdate();
}

void CCh_Op_St::OnSetCurrent(){
	RETSTAT				retSt;
	float				px;
	float				py;
	int					vx;
	int					vy;
	int					miceIndex;
	int					prevType;
	int					type;
	TCHAR				temp[32];
	GENIDX				zoneIdx;
	micro_cmd_exe		mice;
	
	this->m_cmbOp.GetLBText(this->m_cmbOp.GetCurSel(), temp);
	miceIndex = _ttoi(temp);

	retSt = GetMice(miceIndex, &mice);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error reading MICE file."), _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}

	if((!_tcscmp(mice.cmovet, MICRO_MOVE_FAGR_MM))   ||	
	   (!_tcscmp(mice.cmovet, MICRO_MOVE_CLGR_MM))   ||	
	   (!_tcscmp(mice.cmovet, MICRO_MOVE_FAGRL_MM))){
		retSt = GetMastPosInfi(mice.mast, &px, &py, &zoneIdx);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error reading mast position."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
			
		vx = 0;
		vy = 0;

		retSt = GetZonePhisVirtCoord(px, py, &vx, &vy); 
		if(retSt != RT_NORMAL){
			MessageBox(_T("Main mast is not currently on a cell, cannot set this position for this movement."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

		GetCellType(mice.Virt_X, mice.Virt_Y, &prevType);
		GetCellType(vx, vy, &type);

		if(prevType != type){
			MessageBox(_T("Current cell type differs from the planned one. Cannot set this position for this movement."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

		mice.Virt_X = vx;
		mice.Virt_Y = vy;

		retSt = SetMice(mice);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error writing MICE file."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

	}
	else{
		retSt = GetMastPosInfi(mice.mast, &px, &py, &zoneIdx);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error reading mast position."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
		mice.startPX = px;
		mice.startPY = py;

		this->m_startX = px;
		this->m_startY = py;

		retSt = SetMice(mice);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error writing MICE file."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
	}

	this->UpdateData(false);
}

void CCh_Op_St::OnSetCoord(){
	RETSTAT				retSt;
	int					miceIndex;
	int					prevType;
	int					type;
	int					vx;
	int					vy;
	TCHAR				temp[32];
	micro_cmd_exe		mice;
	
	this->UpdateData(true);

	if((this->m_startX == 0) && (this->m_startY == 0)){
		MessageBox(_T("Invalid start coordinates."), _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}
	
	this->m_cmbOp.GetLBText(this->m_cmbOp.GetCurSel(), temp);
	miceIndex = _ttoi(temp);

	retSt = GetMice(miceIndex, &mice);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Error reading MICE file."), _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}

	if((!_tcscmp(mice.cmovet, MICRO_MOVE_FAGR_MM))   ||	
	   (!_tcscmp(mice.cmovet, MICRO_MOVE_CLGR_MM))   ||	
	   (!_tcscmp(mice.cmovet, MICRO_MOVE_FAGRL_MM))){
		vx = 0;
		vy = 0;

		retSt = GetZonePhisVirtCoord(this->m_startX, this->m_startY, &vx, &vy); 
		if(retSt != RT_NORMAL){
			MessageBox(_T("Selected position is not a cell, cannot set this position for this movement."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

		GetCellType(mice.Virt_X, mice.Virt_Y, &prevType);
		GetCellType(vx, vy, &type);

		if(prevType != type){
			MessageBox(_T("Selected cell type differs from the planned one. Cannot set this position for this movement."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

		mice.Virt_X = vx;
		mice.Virt_Y = vy;

		retSt = SetMice(mice);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error writing MICE file."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

	}
	else{
		mice.startPX = this->m_startX;
		mice.startPY = this->m_startY;

		retSt = VotedTagWrite(TWN_BRIDGE_SP, 0, (float)(mice.startPX), FLT_VALUE);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error setting Bridge SP tag."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

		retSt = VotedTagWrite(TWN_TROLLEY_SP, 0, (float)(mice.startPY), FLT_VALUE);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error setting Trolley SP tag."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}

		retSt = SetMice(mice);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error writing MICE file."), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
	}

	this->UpdateData(false);
}
